package ar.com.pragmatia.lanacion;

import java.util.Vector;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import ar.com.pragmatia.lanacion.data.AppData;
import ar.com.pragmatia.lanacion.data.Client;

public class MainClientActivity extends LaNacionActivity {

	private EditText txtCode;
	private EditText txtName;
	private TableLayout tblClients;
	private Dialog dialog;
	private Context appContext;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clients_main);

		appContext = this;

		Button btnClients = (Button) findViewById(R.id.btnClients);
		if (btnClients != null) {
			btnClients.setPressed(true);
		}
		// setListAdapter(new ArrayAdapter<String>(this, R.layout.grid_item,
		// COUNTRIES));

		// GridView gridview = (GridView) findViewById(R.id.gridview);
		// gridview.setAdapter(new ClientsAdapter(this));
		// Client client = new Client();
		// ArrayAdapter<Client> aAdapter = new ArrayAdapter<Client>(this,
		// R.layout.grid_item);
		// aAdapter.addAll((Collection) client.find());
		//
		// gridview.setAdapter(aAdapter);

		txtCode = (EditText) findViewById(R.id.txtCode);
		txtName = (EditText) findViewById(R.id.txtName);
		tblClients = (TableLayout) findViewById(R.id.tblClients);

		txtCode.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				String text = getFilters();
				Client cl = new Client();
				Vector<AppData> v = cl.find(text);
				updateTable(tblClients, v);
			}
		});

		txtName.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				String text = getFilters();
				Client cl = new Client();
				Vector<AppData> v = cl.find(text);
				updateTable(tblClients, v);
			}
		});

		Client client = new Client();
		Vector<AppData> clients = client.find();
		appendRows(tblClients, clients);

		OnClickListener addClient = new OnClickListener() {

			public void onClick(View v) {
				Intent newClientActivity = new Intent().setClass(
						getApplicationContext(), NewClientActivity.class);
				startActivity(newClientActivity);

			}
		};

		Button btnAddClient = (Button) findViewById(R.id.btnAddClient);
		btnAddClient.setOnClickListener(addClient);
	}

	private String getFilters() {
		String filter = "";
		String code = txtCode.getText().toString();
		if (!code.isEmpty()) {
			filter = " WHERE client_code LIKE '%" + code + "%'";
		}
		String name = txtName.getText().toString();
		if (!name.isEmpty()) {
			if (!code.isEmpty()) {
				filter += " AND ";
			} else {
				filter += " WHERE ";
			}
			filter += "shop_name LIKE '%" + name + "%'";
		}
		return filter;
	}

	private void addTableHeader(TableLayout table) {
		TableRow row = new TableRow(this);

		TextView c = new TextView(this);
		c.setText(getText(R.string.code));
		c.setPadding(3, 3, 3, 3);
		row.addView(c);

		c = new TextView(this);
		c.setText(getText(R.string.name));
		c.setPadding(3, 3, 3, 3);
		row.addView(c);

		c = new TextView(this);
		c.setText(getText(R.string.temporal_credit));
		c.setPadding(3, 3, 3, 3);
		row.addView(c);

		c = new TextView(this);
		c.setText(getText(R.string.credit_limit));
		c.setPadding(3, 3, 3, 3);
		row.addView(c);

		c = new TextView(this);
		c.setText(getText(R.string.credit_available));
		c.setPadding(3, 3, 3, 3);
		row.addView(c);

		row.setBackgroundColor(0xeeeeee);

		table.addView(row, new TableLayout.LayoutParams());
	}

	private void updateTable(TableLayout table, Vector<AppData> clients) {
		table.removeAllViews();
		addTableHeader(table);
		appendRows(table, clients);
	}

	private void appendRows(TableLayout table, Vector<AppData> clients) {
		int rowSize = clients.size();
		for (int i = 0; i < rowSize; i++) {
			TableRow row = new TableRow(this);

			TextView c = new TextView(this);
			c.setText(((Client) clients.get(i)).getClientCode());
			c.setPadding(3, 3, 3, 3);
			row.addView(c);

			c = new TextView(this);
			c.setText(((Client) clients.get(i)).getShopName());
			c.setPadding(3, 3, 3, 3);
			row.addView(c);

			c = new TextView(this);
			c.setText(((Client) clients.get(i)).getTemporalCredit());
			c.setPadding(3, 3, 3, 3);
			row.addView(c);

			c = new TextView(this);
			c.setText(((Client) clients.get(i)).getCreditLimit());
			c.setPadding(3, 3, 3, 3);
			row.addView(c);

			c = new TextView(this);
			c.setText(((Client) clients.get(i)).getAvailableCredit());
			c.setPadding(3, 3, 3, 3);
			row.addView(c);

			row.setTag(((Client) clients.get(i)).getId());

			row.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// v.getTag();

					dialog = new Dialog(appContext);
					dialog.setContentView(R.layout.clients_view);

					Client clt = new Client();
					Client client = (Client) clt.find(
							"WHERE id = " + v.getTag()).get(0);

					dialog.setTitle("");

					if (client != null) {
						dialog.setTitle(client.getShopName());

						TextView txtShopName = (TextView) dialog
								.findViewById(R.id.txtShopName);
						txtShopName.setText(client.getShopName());

						TextView txtClientName = (TextView) dialog
								.findViewById(R.id.txtClientName);
						txtClientName.setText(client.getClientName());

						TextView txtShopAddress = (TextView) dialog
								.findViewById(R.id.txtShopAddress);
						txtShopAddress.setText(client.getShopAddress());

						TextView txtLegalCertificate = (TextView) dialog
								.findViewById(R.id.txtLegalCertificate);
						txtLegalCertificate.setText(client
								.getLegalCertificate());

						TextView txtShopPhone = (TextView) dialog
								.findViewById(R.id.txtShopPhone);
						txtShopPhone.setText(client.getTelephone());

						TextView txtAvailability = (TextView) dialog
								.findViewById(R.id.txtAvailability);
						txtAvailability.setText(client.getAvailability());

						TextView txtEmail = (TextView) dialog
								.findViewById(R.id.txtEmail);
						txtEmail.setText(client.getEmail());

						TextView txtClientIdCard = (TextView) dialog
								.findViewById(R.id.txtClientIdCard);
						txtClientIdCard.setText(client.getClientIdCard());

						TextView txtShopPhone2 = (TextView) dialog
								.findViewById(R.id.txtShopPhone2);
						txtShopPhone2.setText(client.getTelephone2());

					}

					// Button cancel = (Button) dialog
					// .findViewById(R.id.btnCancel);
					// cancel.setOnClickListener(new OnClickListener() {
					//
					// @Override
					// public void onClick(View v) {
					// dialog.dismiss();
					//
					// }
					// });
					dialog.show();

				}
			});

			table.addView(row, new TableLayout.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.FILL_PARENT, 1.0f));
		}
	}
}
